﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;

namespace ZaxLib.Web.UI.WebControls
{
	[
	DefaultProperty("Text"),
	ToolboxData("<{0}:GridHeader runat=server></{0}:GridHeader>"),
	ParseChildren(true, "GridItems")
	]
	public class GridHeader : WebControl, IEnumerable, IGridItem
	{
		#region Class Parameters

		private int _idx;
		private ArrayList _gridItems;
		
		#endregion

		#region Tag Parameters

		[Bindable(true)]
		[Category("Appearance")]
		[DefaultValue("")]
		[Localizable(true)]
		public TableRow Data
		{
			get
			{
				TableRow row = ViewState["HeadData"] as TableRow;
				if (row == null)
				{
					row = new TableRow();
					Data = row;
					if (GridItems.Count > 0)
					{
						foreach (GridItem item in GridItems)
						{
							TableHeaderCell th = new TableHeaderCell();
							th.Text = item.Label;
							Data.Controls.Add(th);
						}
					}
				}
				return row;
			}

			set
			{
				ViewState["HeadData"] = value;
			}
		}

		[
		Bindable(true),
		Category("Appearance"),
		DefaultValue(""),
		Localizable(true)
		]
		public String Label
		{
			get
			{
				String s = ViewState["Label"] as String;
				return (s == null ? String.Empty : s);
			}
			set
			{
				ViewState["Label"] = value;
			}
		}

		[
			Category("Behavior"),
			Description("Element in the grid to be drawn"),
			DesignerSerializationVisibility(DesignerSerializationVisibility.Content),
			PersistenceMode(PersistenceMode.InnerDefaultProperty)
		]
		public ArrayList GridItems
		{
			get
			{
				if (_gridItems == null)
				{
					_gridItems = new ArrayList();
				}

				return _gridItems;
			}
		}

		[
		Bindable(true),
		Category("Appearance"),
		DefaultValue("TableHeader"),
		Localizable(true)
		]
		public String CssTitle
		{
			get
			{
				String s = ViewState["CssTitle"] as String;
				return (s == null ? "TableHeader" : s);
			}
			set
			{
				ViewState["CssTitle"] = value;
			}
		}

		#endregion

		#region Render Methods

		public override void RenderControl(HtmlTextWriter writer)
		{
			if (Label.Trim().Length > 0)
			{
				// Create table row header
				TableRow tr = new TableRow();
				TableCell td = new TableCell();
				// Create a div container to put header
				HtmlGenericControl div = new HtmlGenericControl("div");
				// Create H3 tag for page importance
				HtmlGenericControl h3 = new HtmlGenericControl("h3");
				// Add controls to table row
				div.Controls.Add(h3);
				h3.InnerText = Label;
				td.Controls.Add(div);
				// Check if there is a colspan needed
				if (GridItems.Count > 1) td.ColumnSpan = GridItems.Count;
				// Set Css Class to CssTitle
				tr.CssClass = CssTitle;
				tr.Controls.Add(td);
				// Render header row
				tr.RenderControl(writer);
			}

			if (GridItems != null && GridItems.Count > 0)
			{
				TableRow tr = new TableRow();
				tr.CssClass = CssClass;
				foreach (GridItem item in GridItems)
				{
					TableHeaderCell th = new TableHeaderCell();
					th.Text = item.Label;
					String headId = String.Format("{0}{1}", UniqueID, item.Field);
					th.ID = headId;
					// TODO: Add HeadId to list of events to capture
					tr.Controls.Add(th);
				}

				tr.RenderControl(writer);
			}
		}// End method RenderControl

		#endregion

		#region Enumerator Methods

		private GridEnumerator _enumerator = null;

		IEnumerator IEnumerable.GetEnumerator()
		{
			if (_enumerator == null) _enumerator = new GridEnumerator(this);
			return _enumerator;
		}

		#endregion

		#region GridItem Interface Methods

		/// <summary>
		/// Advance to Next record in list
		/// </summary>
		/// <returns></returns>
		public bool Next()
		{
			bool retVal = false;
			if (Index + 1 < Data.Cells.Count)
			{
				retVal = true;
				Index++;
			}

			return retVal;
		}

		/// <summary>
		/// Get/Set current index
		/// </summary>
		public int Index
		{
			get
			{
				return _idx;
			}
			set
			{
				// TODO: Capture out of range index values
				_idx = value;
			}
		}

		/// <summary>
		/// Set list to top
		/// </summary>
		public void Reset()
		{
			Index = -1;
		}

		/// <summary>
		/// Get current data object
		/// </summary>
		public Object Current
		{
			get
			{
				return Data.Cells[Index];
			}
		}

		/// <summary>
		/// Set row data
		/// </summary>
		/// <param name="data">Dictionary of data to add for this row</param>
		public void SetData(Dictionary<String, Object> data)
		{

		}

		#endregion
	}
}
